<?php

abstract class Crud_Form_Element
{
	/**
	 * Wrapped Zend_Form_Element.
	 * 
	 * @var Zend_Form_Element
	 */
	protected $_element;
	
	/**
	 * Create element by given variable.
	 * Can create element via Crud_Db_Table_Column_Abstract.
	 * Can create element via 
	 * 
	 * @return Crud_Form_Element
	 */
	public static function factory()
	{
		$args = func_get_args();
		
		$arg = array_shift($args);
		if ($arg instanceof Crud_Db_Table_Column_Abstract) {
			$column = $arg;
			if ($column->isFK()) {
				if ($column->getFKTable()->getTableName() == 'file') {
					return new Crud_Form_Element_Column_File($column);
				}
				
				return new Crud_Form_Element_Column_ForeignKey($column);
			} elseif ($column->getType() == 'enum') {
				return new Crud_Form_Element_Column_Enum($column);
			}
				
			return new Crud_Form_Element_Column($column);
		}
		
		if ($arg instanceof Crud_Db_Table_Abstract) {
			$table = $arg;
			$m2mTable = array_shift($args);
			return new Crud_Form_Element_Many2many($table, $m2mTable);
		}
		
		throw new Crud_Exception('Cant create element. Unknown arguments given.');
	}
	
	/**
	 * Get wrapped Zend_Form_Element.
	 * 
	 * @return Zend_Form_Element
	 */
	public function getElement()
	{
		return $this->_element;
	}
	
	/**
	 * Set wrapped Zend_Form_Element. 
	 * 
	 * @param Zend_Form_Element
	 * @return Crud_Form_Element
	 */
	public function setElement($element)
	{
		if (!$element instanceof Zend_Form_Element) {
			throw new Crud_Exception('Cant set element. Unknown instance given.');
		}
		
		$this->_element = $element;
		return $this;
	}
}